package com.ipet.app.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AnimationUtils;

import com.github.clans.fab.FloatingActionMenu;
import com.ipet.app.R;
import com.ipet.app.adapter.ViewPagerAdapter;
import com.ipet.app.ui.base.BaseActivity;
import com.ipet.app.ui.fragment.MainOneFragment;
import com.ipet.app.ui.fragment.MainThreeFragment;
import com.ipet.app.ui.fragment.MainTwoFragment;
import com.ipet.app.ui.fragment.TestFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {

    //snackbar在可以滑动的界面中 为了完全显示 可依附在fabMenu上
    public static FloatingActionMenu fabMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initTop();
        initLeft();
        //初始化中间界面的时候,给fb绑定上滑向下取消
        initCenter(initFB());
    }

    /**
     * 初始化 FloatingActionButton
     */

    private FloatingActionMenu initFB() {
        fabMenu = (FloatingActionMenu) findViewById(R.id.fab_menu);
        fabMenu.hideMenuButton(false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                fabMenu.showMenuButton(true);
                fabMenu.setMenuButtonShowAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.show_from_bottom));
                fabMenu.setMenuButtonHideAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.hide_to_bottom));
            }
        }, 300);
        getWindow().getDecorView().findViewById(R.id.fab1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fabMenu.toggle(true);
                Intent intent = new Intent(getApplicationContext(), SendPostActivity.class);
                startActivity(intent);
            }
        });
        getWindow().getDecorView().findViewById(R.id.fab2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fabMenu.toggle(true);
            }
        });
        return fabMenu;
    }

    /**
     * 初始化抽屉
     */
    private void initLeft() {
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        View view =navigationView.getHeaderView(0);
        view.findViewById(R.id.person_information).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), PersonInformation.class);
                startActivity(intent);
            }
        });
        view.findViewById(R.id.follow).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), FollowFanActivity.class);
                startActivity(intent);
            }
        });
        view.findViewById(R.id.fan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), FollowFanActivity.class);
                startActivity(intent);
            }
        });
    }

    /**
     * 初始化toolbar和toggle
     */
    private void initTop() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
    }

    /**
     * 初始化主界面 tablayout viewpager
     */
    private void initCenter(final FloatingActionMenu fabMenu) {
        TabLayout homeTableyout = (TabLayout) findViewById(R.id.tablayout);
        ViewPager homeViewpager = (ViewPager) findViewById(R.id.viewpager);
        String[] mTabsArray = getResources().getStringArray(R.array.home_tabs);
        List<Fragment> mFragments = new ArrayList<>();
        //为MainOneFragment绑定上滑动取消fab监听
        mFragments.add(MainOneFragment.newInstance(fabMenu));
        mFragments.add(MainTwoFragment.newInstance());
        mFragments.add(MainThreeFragment.newInstance());
        mFragments.add(TestFragment.newInstance());
        homeViewpager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), mFragments, mTabsArray));
        //设置viewpager缓存所有fragment
        homeViewpager.setOffscreenPageLimit(mFragments.size());
        homeTableyout.setupWithViewPager(homeViewpager);
        homeViewpager.setCurrentItem(0);
        homeViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                if (position != 0) {
                    fabMenu.hideMenu(true);
                } else {
                    fabMenu.showMenu(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_search) {
            Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();
        Intent intent;

        if (id == R.id.my_messages) {
            intent = new Intent(getApplicationContext(), NewsActivity.class);
            startActivity(intent);
        } else if (id == R.id.my_friends) {

        } else if (id == R.id.my_group) {

        } else if (id == R.id.my_pet) {

        } else if (id == R.id.my_collects) {

        } else if (id == R.id.my_setting) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

}
